import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { InfraDatacenterTableFieldsApi } from '#/api/infra/db/tablefields';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { JAVA_TYPE } from '@vben/constants';

import { Typography } from 'ant-design-vue';

import { getDataTypeDict } from '#/api/infra/db/tablefields';
import { getSimpleDictTypeList } from '#/api/system/dict/type';
import { DictBadge } from '#/components/dict-badge';

const { Paragraph } = Typography;
/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'tableId',
      label: '所属表',
      rules: 'required',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'fieldName',
      label: '字段名称',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入字段名称',
      },
    },
    {
      fieldName: 'fieldDesc',
      label: '字段描述',
      component: 'Input',
      componentProps: {
        placeholder: '请输入字段描述',
      },
    },
    {
      fieldName: 'dataType',
      label: '数据类型',
      rules: 'required',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        api: getDataTypeDict,
        placeholder: '请选择数据类型',
      },
    },
    {
      fieldName: 'columnSize',
      label: '字段长度',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入字段长度',
      },
    },
    {
      fieldName: 'javaType',
      label: 'Java类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: JAVA_TYPE,
        placeholder: '请选择Java类型',
      },
    },
    {
      fieldName: 'javaField',
      label: 'Java属性名',
      component: 'Input',
      componentProps: {
        placeholder: '请输入Java属性名',
      },
    },
    {
      fieldName: 'dictType',
      label: '数据字典类型',
      component: 'ApiSelect',
      componentProps: () => {
        return {
          allowClear: true,
          api: getSimpleDictTypeList,
          placeholder: '请输入字典类型',
          labelField: 'name',
          valueField: 'type',
          showSearch: true,
          optionFilterProp: 'label',
        };
      },
    },
    {
      fieldName: 'required',
      label: '是否必填',
      rules: 'required',
      component: 'RadioGroup',
      defaultValue: false,
      componentProps: {
        options: [
          {
            label: '是',
            value: true,
          },
          {
            label: '否',
            value: false,
          },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
    {
      fieldName: 'primaryKey',
      label: '是否主键',
      rules: 'required',
      component: 'RadioGroup',
      defaultValue: false,
      componentProps: {
        options: [
          {
            label: '是',
            value: true,
          },
          {
            label: '否',
            value: false,
          },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
  ];
}

/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '字段名称',
      field: 'fieldName',
    },
    {
      label: '字段描述',
      field: 'fieldDesc',
    },
    {
      label: '数据类型',
      field: 'dataType',
    },
    {
      label: '字段长度',
      field: 'columnSize',
    },
    {
      label: 'Java类型',
      field: 'javaType',
    },
    {
      label: 'Java属性名',
      field: 'javaField',
    },
    {
      label: '数据字典类型',
      field: 'dictType',
    },
    {
      label: '是否必填',
      field: 'required',
      render: (val) =>
        h(DictBadge, {
          value: val,
        }),
    },
    {
      label: '是否主键',
      field: 'primaryKey',
      render: (val) =>
        h(DictBadge, {
          value: val,
        }),
    },
    {
      label: '扩展项',
      field: 'options',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'tableId',
      label: '所属表',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'fieldName',
      label: '字段名称',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入字段名称',
      },
    },
    {
      fieldName: 'fieldDesc',
      label: '字段描述',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入字段描述',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<InfraDatacenterTableFieldsApi.DatacenterTableFields>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '主键',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'fieldName',
      title: '字段名称',
      minWidth: 120,
    },
    {
      field: 'fieldDesc',
      title: '字段描述',
      minWidth: 120,
    },
    {
      field: 'dataType',
      title: '数据类型',
      minWidth: 120,
    },
    {
      field: 'javaType',
      title: 'Java类型',
      minWidth: 120,
    },
    {
      field: 'javaField',
      title: 'Java属性名',
      minWidth: 120,
    },
    {
      field: 'dictType',
      title: '字典类型',
      minWidth: 120,
    },
    {
      field: 'required',
      title: '是否必填',
      minWidth: 120,
      cellRender: {
        name: 'CellDictBadge',
      },
    },
    {
      field: 'primaryKey',
      title: '是否主键',
      minWidth: 120,
      cellRender: {
        name: 'CellDictBadge',
      },
    },
    {
      field: 'options',
      title: '扩展项',
      minWidth: 120,
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 160,
      formatter: 'formatDateTime',
    },
    {
      field: 'actions',
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
